package com.zhonghe.active4j.filesys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhonghe.active4j.common.controller.BaseController;
import com.zhonghe.active4j.core.annotation.Log;
import com.zhonghe.active4j.core.model.AjaxJson;
import com.zhonghe.active4j.core.model.LogType;
import com.zhonghe.active4j.core.model.PageInfo;
import com.zhonghe.active4j.core.query.QueryUtils;
import com.zhonghe.active4j.core.util.MyBeanUtils;
import com.zhonghe.active4j.core.util.ResponseUtil;
import com.zhonghe.active4j.core.util.ShiroUtils;
import com.zhonghe.active4j.filesys.entity.FileTaskEntity;
import com.zhonghe.active4j.filesys.service.FileTaskService;
import com.zhonghe.active4j.filesys.wrapper.FileTaskWrapper;

import lombok.extern.slf4j.Slf4j;
/**
 * 文件处理器
 * @author gyj
 *
 */
@Controller
@RequestMapping("/filesys/filetask")
@Slf4j
public class FileTaskController  extends BaseController{

	@Autowired
	private FileTaskService fileTaskService;
	
	/**
	 * 跳转到列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(Model model) {
		return "filesys/filetask/filetask_list.html";
	}
	
	/**
	 *  获取表格数据 树形结构
	 * @param sysUserEntity
	 * @param request
	 * @return
	 */
	@RequestMapping("/datagrid")
	@ResponseBody
	public void datagrid(FileTaskEntity sysConfigEntity, PageInfo<FileTaskEntity> page, HttpServletRequest request, HttpServletResponse response) {
		//拼接查询条件
		QueryWrapper<FileTaskEntity> queryWrapper = QueryUtils.installQueryWrapper(sysConfigEntity);
		queryWrapper.orderByAsc("LINE_ID,INDEX_CODE");//按流水排序再按序号排序
		
		//执行查询
		IPage<FileTaskEntity> lstResult = fileTaskService.page(page.getPageEntity(), queryWrapper);

		//结果处理,直接写到客户端
		ResponseUtil.write(response, new FileTaskWrapper(lstResult).wrap());
	}
	
	/**
	 * 跳转到页面
	 * @param sysDepartEntity
	 * @param model
	 * @return
	 */
	@RequestMapping("/add")
	public String add(FileTaskEntity sysConfigEntity, Model model) {
		if(StringUtils.isNotEmpty(sysConfigEntity.getId())) {
			FileTaskEntity config = fileTaskService.getById(sysConfigEntity.getId());
			model.addAttribute("config", config);
			model.addAttribute("disabled","disabled");
		}
		return "filesys/filetask/filetask.html";
	}
	
	/**
	 * 保存方法
	 * @param sysUserEntity
	 * @return
	 */
	@RequestMapping("/save")
	@RequiresPermissions("filesys/filetask/add")
	@ResponseBody
	@Log(type = LogType.save, name = "保存用户信息", memo = "新增或编辑保存了用户信息")
	public AjaxJson save(FileTaskEntity sysConfigEntity, String roleIds)  {
		AjaxJson j = new AjaxJson();
		
		try{
			//后端校验
			if(StringUtils.isEmpty(sysConfigEntity.getIndexCode())) {
				j.setSuccess(false);
				j.setMsg("序号不能为空");
				return j;
			}
			if(StringUtils.isEmpty(sysConfigEntity.getCode())) {
				j.setSuccess(false);
				j.setMsg("编码不能为空");
				return j;
			}
			
			if(StringUtils.isEmpty(sysConfigEntity.getName())) {
				j.setSuccess(false);
				j.setMsg("名称不能为空");
				return j;
			}
			
			if(StringUtils.isEmpty(sysConfigEntity.getStatus())) {
				j.setSuccess(false);
				j.setMsg("状态不能为空");
				return j;
			}
			
			if(StringUtils.isEmpty(sysConfigEntity.getLineCode())) {
				j.setSuccess(false);
				j.setMsg("文件流水编码不能为空");
				return j;
			}
			
			if(StringUtils.isEmpty(sysConfigEntity.getInteractiveCode())) {
				j.setSuccess(false);
				j.setMsg("文件交互编码不能为空");
				return j;
			}
			
			//新增保存
			if(StringUtils.isEmpty(sysConfigEntity.getId())) {
				
				fileTaskService.saveUser(sysConfigEntity, roleIds);
			}
			//编辑保存
			else {
				FileTaskEntity tmp = fileTaskService.getById(sysConfigEntity.getId());
				MyBeanUtils.copyBeanNotNull2Bean(sysConfigEntity, tmp);
				fileTaskService.saveOrUpdateUser(tmp, roleIds);
			}
			
		}catch(Exception e) {
			log.error("保存用户信息报错，错误信息:" + e.getMessage());
			j.setSuccess(false);
			e.printStackTrace();
		}
		
		return j;
	}
	
	/**
	 *  删除操作
	 * @param sysUserEntity
	 * @return
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("filesys:filetask:delete")
	@ResponseBody
	@Log(type = LogType.del, name = "删除用户信息", memo = "删除了用户信息")
	public AjaxJson delete(FileTaskEntity sysConfigEntity) {
		AjaxJson j = new AjaxJson();
		try {
			
			if(StringUtils.isNotEmpty(sysConfigEntity.getId())) {
				String userId = ShiroUtils.getSessionUserId();
				if(StringUtils.equals(userId, sysConfigEntity.getId())) {
					j.setSuccess(false);
					j.setMsg("不允许删除自身账户");
					return j;
				}
				FileTaskEntity item = fileTaskService.getById(sysConfigEntity.getId());
				if(null != item && "1".equals(item.getStatus())) {
					j.setSuccess(false);
					j.setMsg("不允许删除非禁用的数据");
					return j;
				}
				fileTaskService.delete(sysConfigEntity);
			}
			
		}catch(Exception e) {
			log.error("删除用户信息出错，错误信息:" + e.getMessage() );
			j.setSuccess(false);
			e.printStackTrace();
		}
		
		return j;
	}
}
